﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication.Models;

namespace MvcApplication.Controllers
{
    public class LuongController : Controller
    {
        Winapp111020DataContext dataContext;
        public LuongController()
        {
            dataContext = new Winapp111020DataContext();
        }

        /// <summary>
        /// Select list of Luong by id NhanSu
        /// </summary>
        /// <param name="selectedNhanSu">id NhanSu</param>
        /// <returns></returns>
        public ActionResult Index(int selectedNhanSu = 0)
        {
            if (dataContext.NhanSuModels.Count(n => n.id_nhan_su == selectedNhanSu && n.deleted == false) == 0)
            {
                return HttpNotFound("Không tìm thấy nhân sự");
            }
            else
            {
                ViewBag.selectedNhanSu = selectedNhanSu;
                ViewBag.hotenNhanSu = dataContext.NhanSuModels.Single(n => n.id_nhan_su == selectedNhanSu).ho_ten;

                var luongs = from l in dataContext.LuongModels
                             where l.id_nhan_su == selectedNhanSu && l.deleted == false
                             select l;
                return View(luongs);
            }
        }

        /// <summary>
        /// Display page to create new Luong for NhanSu
        /// </summary>
        /// <param name="selectedNhanSu">id NhanSu</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(int selectedNhanSu = 0)
        {
            if (dataContext.NhanSuModels.Count(n => n.id_nhan_su == selectedNhanSu && n.deleted == false) == 0)
            {
                return HttpNotFound("Không tìm thấy nhân sự");
            }
            else
            {
                ViewBag.selectedNhanSu = selectedNhanSu;
                ViewBag.hotenNhanSu = dataContext.NhanSuModels.Single(n => n.id_nhan_su == selectedNhanSu).ho_ten;
                return View();
            }
        }

        [HttpPost]
        public ActionResult CreateSubmit(LuongModel luong)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dataContext.LuongModels.InsertOnSubmit(luong);
                    dataContext.SubmitChanges();
                    return RedirectToAction("Index", new { selectedNhanSu = luong.id_nhan_su });
                }
            }
            catch (Exception exc)
            {
                ModelState.AddModelError("", exc);
            }
            return View(luong);
        }

        /// <summary>
        /// display details of Luong by id
        /// </summary>
        /// <param name="id">id of Luong</param>
        /// <returns></returns>
        public ActionResult Details(int id = 0)
        {
            var luong = dataContext.LuongModels.Single(l => l.id_luong == id && l.deleted == false);
            if (luong == null)
            {
                return HttpNotFound("Không tìm thấy hợp đồng");
            }
            else
            {
                return View(luong);
            }
        }

        /// <summary>
        /// Display details of Luong to be edit
        /// </summary>
        /// <param name="id">id of Luong</param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            var luong = dataContext.LuongModels.Single(l => l.id_luong == id && l.deleted == false);
            if (luong == null)
            {
                return HttpNotFound("Không tìm thấy hợp đồng");
            }
            else
            {
                return View(luong);
            }
        }
        [HttpPost]
        public ActionResult Edit(LuongModel luong)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dataContext.LuongModels.DeleteOnSubmit(dataContext.LuongModels.Single(l=>l.id_luong == luong.id_luong));
                    dataContext.LuongModels.InsertOnSubmit(luong);
                    dataContext.SubmitChanges();
                    return RedirectToAction("Index", "Luong", new {selectedNhanSu = luong.id_nhan_su});
                }
            }
            catch (Exception exc)
            {
                ModelState.AddModelError("", exc);
            }
            return View(luong);
        }

        /// <summary>
        /// Display details of Luong to confirm delete
        /// </summary>
        /// <param name="id">id of Luong</param>
        /// <returns></returns>
        public ActionResult Delete(int id = 0, string submitChangesError = "")
        {
            var luong = dataContext.LuongModels.Single(l => l.id_luong == id);
            if (luong == null)
            {
                return HttpNotFound("Không tìm thấy lương");
            }
            else
            {
                if (submitChangesError != string.Empty)
                {
                    ViewBag.ErrorMessage = "Không thể xóa lương: " + submitChangesError;
                }
                return View(luong);
            }
        }
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                LuongModel luong = dataContext.LuongModels.Single(l => l.id_luong == id);
                luong.deleted = true;
                //dataContext.LuongModels.DeleteOnSubmit(luong);
                dataContext.SubmitChanges();
                return RedirectToAction("Index", new { selectedNhanSu = luong.id_nhan_su });
            }
            catch (Exception exc)
            {
                return RedirectToAction("Delete", new { id = id, submitChangeError = exc.Message});
            }
        }
    }
}
